5 * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * * Neither the name of Sebastian Bergmann nor the names of his
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
39 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
40 * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
41 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
43 * @link http://www.phpunit.de/
44 * @since File available since Release 3.1.0
47 require_once 'PHPUnit/Util/Filter.php';
49 PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
52 * Code Coverage helpers.
56 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
57 * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
58 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
59 * @version Release: 3.3.17
60 * @link http://www.phpunit.de/
61 * @since Class available since Release 3.1.0
64 abstract class PHPUnit_Util_CodeCoverage
66 protected static $lineToTestMap = array();
67 protected static $summary = array();
70 * Returns only the executed lines.
74 * @since Method available since Release 3.3.15
76 public static function getExecutedLines(array $data)
78 return self::getLinesByStatus($data, 1);
82 * Returns only the executable lines.
86 * @since Method available since Release 3.3.15
88 public static function getExecutableLines(array $data)
90 return self::getLinesByStatus($data, array(-1, 1));
94 * Returns only the lines that were not executed.
98 * @since Method available since Release 3.3.15
100 public static function getNotExecutedLines(array $data)
102 return self::getLinesByStatus($data, -1);
106 * Returns only the dead code lines.
110 * @since Method available since Release 3.3.15
112 public static function getDeadLines(array $data)
114 return self::getLinesByStatus($data, -2);
118 * Filters lines by status.
121 * @param array|integer $status
123 * @since Method available since Release 3.3.15
125 protected static function getLinesByStatus(array $data, $status)
127 if (!is_array($status)) {
128 $status = array($status);
131 $isFileCache = array();
134 foreach ($data as $file => $coverage) {
135 if (!isset($isFileCache[$file])) {
136 $isFileCache[$file] = self::isFile($file);
139 if (!$isFileCache[$file]) {
143 $result[$file] = array();
145 foreach ($coverage as $line => $_status) {
146 if (in_array($_status, $status)) {
147 $result[$file][$line] = $_status;
156 * Returns the tests that cover a given line.
159 * @param string $file
160 * @param string $line
161 * @param boolean $clear
164 public static function getCoveringTests(array &$data, $file, $line, $clear = FALSE)
166 if (empty(self::$lineToTestMap) || $clear) {
167 foreach ($data as $test) {
168 foreach ($test['files'] as $_file => $lines) {
169 foreach ($lines as $_line => $flag) {
171 if (!isset(self::$lineToTestMap[$_file][$_line])) {
172 self::$lineToTestMap[$_file][$_line] = array($test['test']);
174 self::$lineToTestMap[$_file][$_line][] = $test['test'];
182 if (isset(self::$lineToTestMap[$file][$line])) {
183 return self::$lineToTestMap[$file][$line];
190 * Returns summarized code coverage data.
192 * Format of the result array:
196 * "/tested/code.php" => array(
197 * linenumber => number of tests that executed the line
203 * @param boolean $clear
206 public static function getSummary(array &$data, $clear = FALSE)
208 if (empty(self::$summary) || $clear) {
209 foreach ($data as $test) {
210 foreach ($test['files'] as $file => $lines) {
211 foreach ($lines as $line => $flag) {
213 if (isset(self::$summary[$file][$line][0])) {
214 self::$summary[$file][$line][] = $test['test'];
216 self::$summary[$file][$line] = array($test['test']);
220 else if (!isset(self::$summary[$file][$line])) {
221 self::$summary[$file][$line] = $flag;
226 if (isset($test['executable'])) {
227 foreach ($test['executable'] as $file => $lines) {
228 foreach ($lines as $line => $flag) {
229 if ($flag == 1 && !isset(self::$summary[$file][$line][0])) {
230 self::$summary[$file][$line] = -1;
233 else if (!isset(self::$summary[$file][$line])) {
234 self::$summary[$file][$line] = $flag;
240 if (isset($test['dead'])) {
241 foreach ($test['dead'] as $file => $lines) {
242 foreach ($lines as $line => $flag) {
243 if ($flag == -2 && !isset(self::$summary[$file][$line][0])) {
244 self::$summary[$file][$line] = -2;
252 return self::$summary;
256 * Returns the coverage statistics for a section of a file.
259 * @param string $filename
260 * @param integer $startLine
261 * @param integer $endLine
263 * @since Method available since Release 3.2.0
265 public static function getStatistics(array &$data, $filename, $startLine = 1, $endLine = FALSE)
271 if (isset($data[$filename])) {
272 if ($endLine == FALSE) {
273 $endLine = count(file($filename));
276 foreach ($data[$filename] as $line => $_data) {
277 if ($line >= $startLine && $line < $endLine) {
278 if (is_array($_data)) {
283 else if ($_data == -1) {
289 if ($locExecutable > 0) {
290 $coverage = ($locExecuted / $locExecutable) * 100;
297 'coverage' => $coverage,
298 'loc' => $endLine - $startLine + 1,
299 'locExecutable' => $locExecutable,
300 'locExecuted' => $locExecuted
305 * Checks whether a file (as seen by Xdebug) is actually a file.
307 * @param string $file
310 public static function isFile($file)
312 if (strpos($file, 'eval()\'d code') ||
313 strpos($file, 'runtime-created function') ||
314 strpos($file, 'assert code') ||
315 strpos($file, 'regexp code')) {
323 * Clears the cached summary information.
325 * @since Method available since Release 3.3.0
327 public static function clearSummary()
329 self::$summary = array();